//
//  JHButtonViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-27.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHButtonViewController.h"
#import "JHButtonCollectionViewCell.h"
#import "JHButtonLunboCollectionViewCell.h"
#import "JHButton.h"
#import "JHButtonlunbo.h"
#import "NetwordHandler.h"
#import "JHXiangQingViewController.h"

@interface JHButtonViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)UICollectionView *specialColltionView1;
@property(nonatomic,retain)UICollectionView *collectionView;

/**
 *  网络数组
 */
@property(nonatomic,retain)NSMutableArray *LunboArr;
@property(nonatomic,retain)NSMutableArray *ButtonArr;

@property(nonatomic,retain)UIScrollView *audioScroll;
@end

@implementation JHButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self craetditu];
}
- (void)craetScoll
{
    
    
}
- (void)getData1
{
    NSString *a = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_focus_image?categoryId=%@&version=3.25.7.1&device=android",self.dataId];
    
    [NetwordHandler requestWithURL:a completion:^(id result) {
        NSDictionary *d = result;
        self.LunboArr = [NSMutableArray array];
        NSArray *a1 = [d objectForKey:@"list"];
        for (NSDictionary *a3 in a1) {
            JHButtonlunbo *a4 = [[JHButtonlunbo alloc] initWithDic:a3];
            [_LunboArr addObject:a4];
        }
        [self createScrollView];
    }];
}
- (void)getData2
{
    NSString *s1 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?device=android&per_page=100&category=%@&type=album&page=1",self.str];
//    NSLog(@"%@",s1);
    
    [NetwordHandler requestWithURL:s1 completion:^(id result) {
        NSDictionary *d = result;
        self.ButtonArr = [NSMutableArray array];
        NSMutableArray * a1 = [d objectForKey:@"list"];
        for (NSDictionary *d2 in a1) {
            JHButton *bu = [[JHButton alloc] initWithDic:d2];
            [_ButtonArr addObject:bu];
        }
        [self craetButton];
    }];
}
- (void)craetditu
{
    /**
     页面
     
     :returns: 底图滚动图
     */
    self.audioScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    _audioScroll.bounces = NO;
    //    NSLog(@"%lf",_audioTableView.contentSize.height);
    [self.view addSubview:_audioScroll];
    
}

- (void)setStr:(NSString *)str
{
    if (_str != str) {
        [_str release];
        _str = [str retain];
    }
    [self getData1];
    [self getData2];
}

- (void)createScrollView
{
    //  创建轮播图coll
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width - 20, self.view.frame.size.height / 3);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    self.specialColltionView1 = [[[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 3) collectionViewLayout:layout] autorelease];
    _specialColltionView1.pagingEnabled = YES;
    _specialColltionView1.backgroundColor = [UIColor whiteColor];
    _specialColltionView1.userInteractionEnabled = NO;
    _specialColltionView1.delegate = self;
    _specialColltionView1.dataSource = self;
    [_audioScroll addSubview:_specialColltionView1];
    [_specialColltionView1 registerClass:[JHButtonLunboCollectionViewCell class] forCellWithReuseIdentifier:@"special"];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(runPhotoes:) userInfo:nil repeats:YES];
    
}
- (void)runPhotoes:(NSTimer *)time
{
    //    NSInteger i = 0;
    
    _specialColltionView1.contentOffset = CGPointMake(_specialColltionView1.contentOffset.x +(self.view.frame.size.width - 20), 0);
    
    if (_specialColltionView1.contentOffset.x == (self.view.frame.size.width - 20)*6) {
        _specialColltionView1.contentOffset = CGPointMake(self.view.frame.size.width - 20, 0);
        //        i = 0;
    }
    
}
- (void)craetButton
{
    /**
     *  页面
     *
     *  @return 按钮
     */
    //1.设置collectionView布局信息
    //苹果系统提供的一个瀑布流效果
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //layout设置
    //item大小
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 5, self.view.frame.size.width / 5 + 40);
    //    //item之间最小行间距
    //    flowLayout.minimumLineSpacing = 10;
    //    //item之间最小列间距
    //    flowLayout.minimumInteritemSpacing = 10;
    //    //滑动方向(默认为垂直滑动)
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    //item的边界大小 (上左下右)
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //2.1创建collectionView

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height / 3 + 20, self.view.frame.size.width, self.view.frame.size.height / 3  * 2 - 40) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    //2.2设置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //2.3添加父视图
    [_audioScroll addSubview:self.collectionView];
    //2.4内存管理
    [_collectionView release];
    [flowLayout release];
    //#warning collectionView创建时必须先注册一个cell
    [self.collectionView registerClass:[JHButtonCollectionViewCell class] forCellWithReuseIdentifier:@"my"];
    

}
//  轮播图
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //  轮播图行数
//    return _lunbotuArr.count;
    _audioScroll.contentSize = CGSizeMake(0,self.view.frame.size.height / 3 + 20 + _collectionView.contentSize.height);
    
    if (collectionView == _specialColltionView1) {
        return _LunboArr.count;
    }else
        if (collectionView == _collectionView) {
            return _ButtonArr.count;
        }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //  轮播图赋值
    if (collectionView == _specialColltionView1) {
    JHButtonLunboCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"special" forIndexPath:indexPath];
    cell.lunbo = _LunboArr[indexPath.row];
    return cell;
    }else
        if (collectionView == _collectionView) {
            JHButtonCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"my" forIndexPath:indexPath];
            cell.button = _ButtonArr[indexPath.row];
            return cell;
        }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //  轮播图触发方法
    if (collectionView == _collectionView) {
        JHXiangQingViewController *a = [[JHXiangQingViewController alloc] init];
        a.name = [_ButtonArr[indexPath.row] tname];
        a.title = self.str;
        a.name2 =[_ButtonArr[indexPath.row] tname];
        [self.navigationController pushViewController:a animated:YES];
    }else
        if (collectionView == _specialColltionView1) {
            NSLog(@"点击");
        }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
